package com.wc.codeforces.数学.推导.Insane_Problem;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/12/24 16:40
 * @description https://codeforces.com/contest/2044/problem/E
 */
public class Main {
    /**
     * 思路：
     * 求出 y / x 的范围
     * y_min / x_max <= y / x <= y_max / x_min
     * 求出 n 的范围
     * log_k_(y / x)
     * 遍历每一个 n, 找到, x范围中满足y的范围
     * 二分
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int k, l1, l2, r1, r2;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            k = sc.nextInt();
            l1 = sc.nextInt();
            r1 = sc.nextInt();
            l2 = sc.nextInt();
            r2 = sc.nextInt();
            // 求出 y / x 的范围
            int L = (l2 + r1 - 1) / r1, R = r2 / l1;

            // 求出 n 的范围
            L = (int) (Math.log(L) / Math.log(k) + 0.5);
            R = (int) (Math.log(R) / Math.log(k));

            long K = 1;
            // K = k ^ L
            for (int i = 1; i <= L; i++) K = K * k;

            int res = 0;
            // K = k ^ n
            for (int n = L; n <= R; n++, K *= k) {
                int l = l1, r = r1;
                while (l < r) {
                    int mid = l + r + 1 >> 1;
                    if (K * mid <= r2) l = mid;
                    else r = mid - 1;
                }
                // 超出边界说明不存在
                if (K * r < l2) continue;

                int b = r;
                l = l1;
                r = r1;
                while (l < r) {
                    int mid = l + r >> 1;
                    if (K * mid >= l2) r = mid;
                    else l = mid + 1;
                }

                // 超出边界说明不存在
                if (K * r > r2) continue;
                int a = r;
                res += b - a + 1;

            }
            out.println(res);
        }
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

